package com.example.android.fragments

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.example.android.R
import com.example.android.extensions.Alert
import com.example.android.extensions.AlertDialog
import com.example.android.extensions.Services
import com.example.android.utils.models.MyResult
import kotlinx.android.synthetic.main.fragment_select_bed2.*
import kotlinx.android.synthetic.main.fragment_select_bed3.*
import kotlinx.android.synthetic.main.fragment_select_bed_finish.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [SelectBed3Fragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class SelectBedFinishFragment() : Fragment() {
    // TODO: Rename and change types of parameters
//    val dormId = dormId
//    val bedId = bedId
//    val region = region
//    val dormNumber = dormNumber


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_select_bed_finish, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        lifecycleScope.launch(Dispatchers.Main) {
            Log.d(toString(), "asd")
            val data = Services.SelectBedService.Check()
            val text = view.findViewById<TextView>(R.id.text)
            text.text = "你已选择${data.Data?.id}号床 ${data.Data?.region} ${data.Data?.dormNumber}"

        }



//        val stu_name_text = findViewById<TextView>(R.id.stu_name)
//
//        val builder = AlertDialog.Builder(stu_name_text.context)
//            //val builder: AlertDialog.Builder = AlertDialog.Builder(this)
//        builder.setTitle("尊敬的用户")
//        builder.setMessage("你真的要卸载我吗？")

        //val next_button = view.findViewById<Button>(R.id.next)
        change.setOnClickListener {
            AlertDialog {
                it.SetContent("你确定要更改寝室吗？")
                it.SetOnSureButton {
                    it.dismiss()
                    lifecycleScope.launch {
                        val data = Services.SelectBedService.DeleteBed()
                        Log.d(toString(),"!!!!!!!!!!!!!!!!!!!!!!!!"+data.Code.toString())
                        //Log.d(toString(),MyResult.OK.toString())
                        if(data.Code == MyResult.OK){
                            val select_bed_step1 = SelectBedFragment()

                            requireActivity().supportFragmentManager
                                .beginTransaction()
                                .setCustomAnimations(
                                    R.anim.fragment_item_select_in,
                                    R.anim.fragment_item_select_out,
                                    R.anim.fragment_item_back_in,
                                    R.anim.fragment_item_back_out,
                                )
                                .replace(R.id.content_main_fragment,select_bed_step1)
                                .addToBackStack(null)
                                .commit()
                        }
                        else{
                            Alert("不可以更换寝室：${data.Message}")
                        }
                    }


                }
                it.SetOnCancelButton {
                    it.dismiss()
                }
            }

        }







    }



}